<?php
namespace Health\Http\Controllers\Admin;

use Health\Http\Controllers\Admin\BaseController;
use Health\Models\Goods;
use Health\Models\Category;
use Auth;
use Illuminate\Http\Request;
use Health\Http\Requests\GoodsRequest;

class GoodsController extends BaseController
{
    public function index(Request $request)
    {
        $searchData=array_map('trim', $request->only(['goods_category_id','goods_name','status']));
        $data['searchData']=$searchData;
        $where = [];

        //商品状态
        if (!empty($searchData['status'])) {
            $where[] = array('goods.status', $searchData['status']);
        }
        //商品分类搜索字段
        if (!empty($searchData['goods_category_id'])) {
                $where[] = array('goods_category_id', $searchData['goods_category_id']);
        }
        //商品名称搜索字段
        if (!empty($searchData['goods_name'])) {
            $where[] = ['goods_name', 'like', '%' . $searchData['goods_name'] . '%'];
        }
        $data['data']=Goods::leftJoin('goods_category', 'goods_category.id', '=', 'goods.goods_category_id')
            ->where($where)
            ->select('goods.*','goods_category.category_name')
            ->orderBy('status','desc')
            ->orderBy('sort','desc')
            ->paginate(10);
        $data['category']=Category::getCategory();
        return view('admin.goods.index',$data);
    }

    public function add()
    {
        $data['category']=Category::getCategory();
        return view('admin.goods.edit',$data);
    }

    public function edit($id)
    {
        $data['data']=Goods::find($id);
        $data['category']=Category::getCategory();
        return view('admin.goods.edit',$data);
    }

    public function postEdit(GoodsRequest $request)
    {
        $data=array_map('trim', $request->except(['_url', '_','img1','img2','img3','submit']));

        if($img1=$this->uploadImg($request->file('img1'))){
            $data['goods_pic1_url']=$img1;
        }
        if($img2=$this->uploadImg($request->file('img2'))){
            $data['goods_pic2_url']=$img2;
        }
        if($img3=$this->uploadImg($request->file('img3'))){
            $data['goods_pic3_url']=$img3;
        }
        Goods::updateOrCreate(['id'=>array_get($data,'id')],$data);
        return redirect('/goods');

    }

    public function delete($id)
    {
        Goods::where(['id'=>$id])->update(['status'=>-2]);
        return redirect('/goods');
    }

    public function updateStatus($id)
    {
        $goods=Goods::find($id);
        //上架转下架
        if($goods->status==1){
            Goods::where(['id'=>$id])->update(['status'=>-1]);
            return ['status'=>'success','data'=>config('admin.goods.status')[-1],'msg'=>'操作成功'];
        }
        //下架转上架
        if($goods->status==-1){
            Goods::where(['id'=>$id])->update(['status'=>1]);
            return ['status'=>'success','data'=>config('admin.goods.status')[1],'msg'=>'操作成功'];
        }


    }

    private function uploadImg($file){
        if ($file&&$file->isValid()) {
            $picName = $file->getClientOriginalName();
            $file->move('images/goods/uploads', $picName);
            return '/images/goods/uploads/' . $picName;
        }
    }
}